import {Restivus} from 'meteor/nimble:restivus';
import {Meteor} from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { FS } from 'meteor/cfs:base-package';
import {Gateways} from '../imports/api/gateways';
if (Meteor.isServer) {
    Meteor.startup(() => {
        let Api = new Restivus({
            useDefaultAuth: true,
            prettyJson: true,
        });
        // 根据触发类型查找设备
        Api.addRoute('findDevicesByTrigger', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const token = param.token;
                        const familyId = param.familyId;
                        const type = param.type;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":token});
                        if(user) {
                            const gws = Gateways.find({'familyId': familyId}).fetch();
                            let devices = [];
                            console.log('');
                            gws.forEach((gw) => { 
                                let dvs = gw.devices;
                                dvs.forEach((dv) => {
                                    if(dv.isSensor == true && dv.sensorType == type){
                                        devices.push(dv);
                                    }
                                })
                            })
                            return {success: true, result: {devices: devices, error: ''}, resultCode: 1000}
                        }else{
                            return {success: false, result: '未获取到登录用户的信息！', resultCode: 1002};
                        }
                    }
                    return {success: false, result: '没有填写信息！', resultCode: 1002};
                }
            }
        });
    });
}